<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title> 权限检测 </title>
    <script src="./mutual.js"></script>
    <style>
        #show{
            width: 80%;
            height: 450px;
            border: 1px solid #cccccc;
            font-size: 16px;
            font-weight: 500;
            margin: 20px auto;
            padding: 15px 10px;
            font-family: 'Microsoft Yahei'
        }
        .item, .shareItem{
            width: 100px;
            height: 35px;
            margin-right: 20px;
            margin-top: 20px;
            border-radius: 5px;
            line-height: 35px;
            border: 1px solid #cccccc;
        }
        .mutual{
            display: flex;
            text-align: center;
            justify-content: flex-start;
            flex-wrap: wrap;
            width: 100%;
        }
    </style>
</head>
<body>
    <div class="mutual">
            <div class="item record">录音权限</div>
            <div class="item camera">相机权限</div>
            <div class="item album">相册权限</div>
            <div class="item storage">存储权限</div>
            <div class="shareItem normal">调起分享面板</div>
            <div class="shareItem fast">微信好友</div>
            <div class="shareItem fast">微信朋友圈</div>
            <div class="shareItem fast">qq好友</div>
            <div class="shareItem fast">qq空间</div>
            <div class="shareItem fast">复制链接</div>
            <div class="shareItem pic">分享图片</div>
            <div class="shareItem miniProgram">分享到小程序</div>
        </div>
    <div id="show">信息面板:<br></div>
    <script>
    function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

    var invokeMutual = {
        power: function power(powerCode, callback) {
            var hasPower = xesApp.checkPermission(powerCode);
            if (hasPower == 1) {
                callback(0);
            } else if (hasPower == 2) {
                xesApp.powerHandler = callback;

                xesApp.applyPermission(powerCode, 'xesApp.powerHandler');
            }
        },
        share: function share(params, callback) {
            try {
                xesApp.shareHandler = callback;
                xesApp.start('xesShare/share', JSON.stringify(params), 'xesApp.shareHandler');
            } catch (err) {
                console.log(err);
            }
        }
    };
    function mutualShare(mode, key) {
        show.innerHTML += '开始分享' + '<br>';
        shareInfo = {
            shareType: 1,
            title: 'test',
            description: 'a test',
            actionUrl: 'https://www.xueersi.com',
            imagePath: 'https://b-ssl.duitang.com/uploads/item/201208/14/20120814165632_Qa8RU.jpeg',
            imageLocalPath: '',
            shareScence: 31,
            businessId: 2018,
            agentKey: 'hello world',
            isOneKeyShare: false,
            wxMiniId: 'gh_1b6d03a727b2',
            wxMiniPath: '/pages/index',
            wxMiniProgramType: 0
        };
        var fastShareInfo = {};
        var picInfo = {};
        var miniProgramInfo = {};
        key = Math.pow(2, key);
        Object.assign(fastShareInfo, shareInfo, {
            shareScence: key,
            isOneKeyShare: true
        });
        Object.assign(picInfo, shareInfo, {
            shareType: 2
        });
        Object.assign(miniProgramInfo, shareInfo, {
            shareType: 3,
            shareScence: 1
        });
        if (mode === 'fast') {
            invokeMutual.share(fastShareInfo, shareHandler);
        } else if (mode === 'normal') {
            invokeMutual.share(shareInfo, shareHandler);
        } else if (mode === 'miniProgram') {
            invokeMutual.share(miniProgramInfo, shareHandler);
        } else if (mode === 'pic') {
            invokeMutual.share(picInfo, shareHandler);
        }
        function shareHandler(result) {
            show.innerHTML += '执行回调结果为：' + result + '<br>';
            if (result == 1) {
                show.innerHTML += '您已成功分享' + '<br>';
            } else if (result == 2) {
                show.innerHTML += '分享失败' + '<br>';
            } else if (result == 3) {
                show.innerHTML += '分享取消' + '<br>';
            }
        }
    }
    function powerhandler(result) {
        show.innerHTML += '权限申请' + '<br>';
        if (result == 0) {
            show.innerHTML += '您已有此项权限<br>';
        } else if (result == 1) {
            show.innerHTML += '权限申请成功<br>';
        } else if (result == 2) {
            show.innerHTML += '权限申请失败<br>';
        }
    }
    window.onload = function () {
        var show = document.getElementById('show'),
            mutual = document.getElementsByClassName('mutual')[0],
            fastShare = [].concat(_toConsumableArray(document.getElementsByClassName('fast')));
        fastShare.map(function (x, i) {
            x.addEventListener('click', function (e) {
                try {
                    mutualShare('fast', i);
                } catch (error) {
                    show.innerHTML += error;
                }
            });
        });
        mutual.addEventListener('click', function (e) {
            if (e.target.classList.contains('record')) {
                invokeMutual.power(202, powerhandler);
            } else if (e.target.classList.contains('camera')) {
                invokeMutual.power(201, powerhandler);
            } else if (e.target.classList.contains('album')) {
                invokeMutual.power(206, powerhandler);
            } else if (e.target.classList.contains('storage')) {
                invokeMutual.power(205, powerhandler);
            } else if (e.target.classList.contains('pic')) {
                mutualShare('pic');
            } else if (e.target.classList.contains('normal')) {
                mutualShare('normal');
            } else if (e.target.classList.contains('miniProgram')) {
                mutualShare('miniProgram');
            }
        });
    };
    </script>
</body>
</html>